package com.ruoyi.bearing.controller;

import com.ruoyi.bearing.domain.ModelInfoMangment;
import com.ruoyi.bearing.domain.ModelTest;
import com.ruoyi.bearing.domain.ModelTestResult;
import com.ruoyi.bearing.service.IModelTestService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DiagnosisRules;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DiagnosisTest;
import com.ruoyi.common.enums.QueryState;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.exception.flask.FlaskException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 模型测试Controller
 */
@Api("模型测试")
@RestController
@RequestMapping("/model/test")
public class ModelTestController extends BaseController {
    @Autowired
    IModelTestService iModelTestService;

    /**
     * 模型测试
     */
    @ApiOperation("故障诊断与寿命预测模型测试")
    @PreAuthorize("@ss.hasPermi('model:test:list')")
    @PostMapping("/testPerformance")
    public AjaxResult testPerformance(@RequestParam("files") MultipartFile[] files, ModelTest modelTest) throws IOException, IllegalAccessException {
        String res = iModelTestService.startModelTest(files[0], modelTest);
        if (res.equals(QueryState.FAIL.getInfo())) {
            throw new BaseException("模型测试异常");
        }
        if (res.equals(QueryState.errorFile.getInfo())) {
            throw new BaseException("请检查文件格式");
        }
        return AjaxResult.success(res);
    }

    /**
     * 查询模型测试记录
     */
    @ApiOperation("查询模型测试记录")
    @PreAuthorize("@ss.hasPermi('model:test:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModelTest modelTest) {
        startPage();
        List<ModelTest> list = iModelTestService.selectModelTest(modelTest);
        return getDataTable(list);
    }

    /**
     * 查询模型列表
     */
    @ApiOperation("查询测试结果")
    @PreAuthorize("@ss.hasPermi('model:test:query')")
    @GetMapping("/query")
    public TableDataInfo query(ModelTest modelTest) {
        startPage();
        List<ModelTestResult> list = iModelTestService.selectModelTestResult(modelTest.getMid());
        for (ModelTestResult modelTestResult : list) {
            if (modelTestResult.getResult().equals("1")) {
                modelTestResult.setResult("一般");
            } else {
                modelTestResult.setResult("正常");
            }
            String type = modelTestResult.getMetrics();
            if (type.equals("Accuracy")) {
                modelTestResult.setDescription(DiagnosisTest.Accuracy.getInfo());
            }
            if (type.equals("Precision")) {
                modelTestResult.setDescription(DiagnosisTest.Precision.getInfo());
            }
            if (type.equals("Recall")) {
                modelTestResult.setDescription(DiagnosisTest.Recall.getInfo());
            }
            if (type.equals("F1")) {
                modelTestResult.setDescription(DiagnosisTest.Score.getInfo());
            }
        }
        return getDataTable(list);
    }

    /**
     * 下载模型测试结果
     */
    @ApiOperation("下载模型测试结果")
    @PreAuthorize("@ss.hasPermi('model:test:export')")
    @Log(title = "下载模型测试结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModelTestResult modelTestResult) {
        List<ModelTestResult> list = iModelTestService.selectModelTestResult(modelTestResult.getMid());
        ExcelUtil<ModelTestResult> util = new ExcelUtil<ModelTestResult>(ModelTestResult.class);
        util.exportExcel(response, list, "模型测试结果");
    }

    /**
     * 下载模型测试记录
     */
    @ApiOperation("下载模型测试记录")
    @PreAuthorize("@ss.hasPermi('model:test:exportModelTest')")
    @Log(title = "下载模型测试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/exportModelTest")
    public void exportModelTest(HttpServletResponse response, ModelTest modelTest) {
        List<ModelTest> list = iModelTestService.selectModelTest(modelTest);
        ExcelUtil<ModelTest> util = new ExcelUtil<ModelTest>(ModelTest.class);
        util.exportExcel(response, list, "模型测试记录");
    }

    /**
     * 下载模型测试历史结果
     */
    @ApiOperation("下载模型测试记录")
    @PreAuthorize("@ss.hasPermi('model:test:exportModelTest')")
    @Log(title = "下载模型测试历史结果", businessType = BusinessType.EXPORT)
    @PostMapping("/download")
    public void exportModelTestResult(HttpServletResponse response, ModelTest modelTest) {
        List<ModelTestResult> list = iModelTestService.selectModelTestResult(modelTest.getMid());
        ExcelUtil<ModelTestResult> util = new ExcelUtil<ModelTestResult>(ModelTestResult.class);
        util.exportExcel(response, list, "模型测试历史结果");
    }

    /**
     * 删除模型训练信息
     */
    @ApiOperation("删除模型训练信息")
    @PreAuthorize("@ss.hasPermi('model:test:remove')")
    @Log(title = "模型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{mids}")
    public AjaxResult remove(@PathVariable String[] mids) {
        return toAjax(iModelTestService.deleteModelTestByMids(mids));
    }
}
